<?php
/**
 * 小说Dao
 */

class Dao_NovelChapterModel extends Dao_BaseModel {

    protected $_table = 'novel_chapter';

    const NORMAL_STATUS = 1;
    const REDIS_KEYWORD = 'NOVEL:CHAPTER:NOVEL_CHAPTER_KEY_';
    const EXTIME        = '86400';

    public function __construct ()
    {
        parent::__construct ();
    }

    /**
     * 获取小说章节列表.
     * @param $wheres
     * @return array|bool
     */
    public function getNovelChapterList ($wheres = [])
    {
        if(!isset($wheres['chapter_novel_id']) && !isset($wheres['chapter_id'])){
            return false;
        }
        
        $cache_key = $this->getCacheKey( self::REDIS_KEYWORD );
        $where    = array ();
        $param    = array ();
        if( isset( $wheres['chapter_novel_id'] ) ){
            $info = $this->getCacheByKey( $cache_key . $wheres['chapter_novel_id'] );
            if( $info ){
                return $info;
            }
            $where[]  = array ( 'chapter_novel_id' , $wheres['chapter_novel_id'] );
            $param[]  = array ( 'chapter_novel_id' , $wheres['chapter_novel_id'] );
        }
        if( $wheres['chapter_id'] && !$wheres['chapter_novel_id'] ){
            $where[]  = array ( 'chapter_id' , $wheres['chapter_id'] );
            $chapter = parent::getOne( array ( 'chapter_novel_id' , 'chapter_id' ) , $where );
            if($chapter){
                $param[]  = array ( 'chapter_novel_id' , $chapter['chapter_novel_id'] );
            }
        }
        
//        $where[]  = array ( 'novel_status' , self::NORMAL_STATUS );
        $order    = [ 'chapter_order' => 'ASC' ];
        
        $limit  = 10;
        $offset = 0;
        if( isset( $wheres['page'] ) && $wheres['page'] > 0){
            $offset = ($wheres['page'] - 1) * 10;
            $this->limit( $limit , $offset );
        }
        
        $info = parent::getRows ( array ( 'chapter_id' , 'chapter_novel_id' , 'chapter_name' , 'chapter_order' , 'chapter_ispay' ) , $param , $order );
        
        $novel_dao = new Dao_NovelModel();
        $novel_param = array('novel_id' => $param[0][1]);
        $novel = $novel_dao->getNovelInfo( $novel_param );
        if( $novel['novel_charge'] == '1'){
            $novel_charge_dao = new Dao_NovelChargeModel();
            $charge = $novel_charge_dao->getByNovelId( $novel_param );
            if( $charge ){
                if( $charge['charge_model'] == 1 ){
                    foreach( $info as $k=>$v ){
                        if( $v['chapter_order'] >= $charge['charge_start_chapter'] ){
                            $info[$k]['chapter_ispay'] = 1;
                        }else{
                            $info[$k]['chapter_ispay'] = 0;
                        }
                    }
                }elseif( $charge['charge_model'] == 2 ){
                    foreach( $info as $k=>$v ){
                        $info[$k]['chapter_ispay'] = 1;
                    }
                }
            }
        }
        if( $info ){
            $this->setCache( $cache_key.$wheres['chapter_novel_id'] , $info , 0 );
        }
        return $info;
    }
    
    /**
     * 获取章节列表.
     * @param $wheres
     * @return array|bool
     */
    public function getChapterList ($wheres = [])
    {
        $where    = array ();
        if( $wheres['chapterids'] ){
            $where[] = $wheres['chapterids'];
        }
        $info = parent::getRows ( array ( 'chapter_id' , 'chapter_novel_id' , 'chapter_name' ) , $where );
        return $info;
    }
    
    /**
     * 获取章节信息.
     * @param $wheres
     * @return array|bool
     */
    public function getChapterInfo ($wheres = [])
    {
        $where    = array ();
        if( $wheres['chapterid'] ){
            $where[] = $wheres['chapterid'];
        }
        $info = parent::getOne ( array ( 'chapter_id' , 'chapter_novel_id' , 'chapter_name','charge_price' ) , $where );
        return $info;
    }
    
}
